package com.blueheart.music.common;

import com.blueheart.music.exception.BaseExceptionEnum;

/**
 * 响应工具类
 */
public class ResultUtil<T>{
    private Integer code;

    private String msg;

    private T data;

    private static final int OK_CODE = 10000;

    private static final String OK_MSG = "SUCCESS";

    public ResultUtil(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultUtil(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultUtil() {
        this(OK_CODE, OK_MSG);
    }

    public static <T> ResultUtil<T> success() {
        return new ResultUtil<>();
    }

    public static <T> ResultUtil<T> success(T result) {
        ResultUtil<T> response = new ResultUtil<>();
        response.setData(result);
        return response;
    }

    public static <T> ResultUtil<T> error(Integer code, String msg) {
        return new ResultUtil<>(code, msg);
    }

    public static <T> ResultUtil<T> error(BaseExceptionEnum ex) {
        return new ResultUtil<>(ex.getCode(), ex.getMsg());
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
}
